﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.DtoModel.Common;
using SystemCenterSoa.DtoModel.Contract;
using SystemCenterSoa.IRepository.ContractManger;
using SystemCenterSoa.PoModel.ContractModel;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.Repository.ContractManger
{
    public class ContractRepository : BaseRepository, ContractIRepository
    {
        public ContractRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {


        }
        public bool AgreementAdd(AgreementPo info)
        {
            _systemCenterDbSoaContext.AgreementPo.Add(info);
            _systemCenterDbSoaContext.SaveChanges();
            return true;
        }

        public bool ContractAdd(ContractPo info)
        {
            _systemCenterDbSoaContext.ContractPo.Add(info);
            _systemCenterDbSoaContext.SaveChanges();
            return true;
        }

        public List<Contype> GetContype()
        {
            var list = _systemCenterDbSoaContext.Contype.ToList();
            return list;
        }

        private void DiStruct(StructurePo db,List<StructurePo> info)
        {
            List<StructurePo> sonList = info.Where(c => c.structure_lid == db.structure_id).ToList();
            if(sonList.Count>0&&sonList!=null)
            {
                db.sonList = new List<StructurePo>();
                foreach (var item in sonList)
                {
                    DiStruct(item, info);
                    db.sonList.Add(item);
                }
            }
        }

        public List<StructurePo> GetStruct()
        {
            List<StructurePo> list = _systemCenterDbSoaContext.StructurePo.ToList();
            List<StructurePo> info = list.Where(p => p.structure_lid == 0).ToList();
            foreach (var item in info)
            {
                DiStruct(item, list);
            }
            return info;
        }

        private void DiArchite(ArchitecturePo db, List<ArchitecturePo> info)
        {
            List<ArchitecturePo> sonList = info.Where(c => c.architecture_lid == db.architecture_id).ToList();
            if (sonList.Count > 0 && sonList != null)
            {
                db.sonList = new List<ArchitecturePo>();
                foreach (var item in sonList)
                {
                    DiArchite(item, info);
                    db.sonList.Add(item);
                }
            }
        }

        public List<ArchitecturePo> GetArchite()
        {
            List<ArchitecturePo> list = _systemCenterDbSoaContext.ArchitecturePo.ToList();
            List<ArchitecturePo> info = list.Where(p => p.architecture_lid == 0).ToList();
            foreach (var item in info)
            {
                DiArchite(item, list);
            }
            return info;
        }

        public PagerResult<ContractPo> GetContract(SearchModel searchModel)
        {
            var list =from A in _systemCenterDbSoaContext.ContractPo
                      select new ContractPo { 
                          chai_sjsum=A.chai_sjsum,
                          chai_sum=A.chai_sum,
                          contract_adder=A.contract_adder,
                          contract_arch=A.contract_arch,
                          contract_area=A.contract_area,
                          contract_conname=A.contract_conname,
                          contract_const=A.contract_const,
                          contract_contname=A.contract_contname,
                          contract_contype=A.contract_contype,
                          contract_first_people=A.contract_first_people,
                          contract_first_tell=A.contract_first_tell,
                          contract_first_type=A.contract_first_type,
                          contract_id=A.contract_id,
                          contract_jgtype=A.contract_jgtype,
                          contract_jzfl=A.contract_jzfl,
                          contract_many=A.contract_many,
                          contract_projectname=A.contract_projectname,
                          contract_projecttell=A.contract_projecttell,
                          contract_reamk=A.contract_reamk,
                          contract_serial=A.contract_serial,
                          contract_sjmany=A.contract_sjmany,
                          contract_textserial=A.contract_textserial,
                          contract_time=A.contract_time,
                          contract_zicompany=A.contract_zicompany,
                          contract_zt=A.contract_zt,
                          img_sjsum=A.img_sjsum,
                          img_sum=A.img_sum
                      };
            if (searchModel.SearchItem != null && searchModel.SearchItem.Count > 0)
            {
                Dictionary<string, string> tempDi = searchModel.SearchItem;
                string name = "name";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(name) && !string.IsNullOrEmpty(tempDi[name]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list = list.Where(c => c.contract_contname.Contains(tempDi[name]));
                }
                string fzname = "fzname";
                //判断搜索条件中是否有user_name
                if (tempDi.Keys.Contains(fzname) && !string.IsNullOrEmpty(tempDi[fzname]))
                {
                    //类似于like查询, 判断userName是否包含查询条件中的条件值
                    list =list.Where(c => c.contract_projectname.Contains(tempDi[fzname]));
                }
            }
                return BasePageSearch(list, searchModel);
        }

        public List<AgreementPo> GetAgreelast()
        {
            var list = _systemCenterDbSoaContext.AgreementPo.ToList();
            int n = list.Count();
            list = list.Where(c => c.agreement_id == n).ToList();
            return list.ToList();
        }

        public AgreementPo LastCha(int id)
        {
            var list= _systemCenterDbSoaContext.AgreementPo.FirstOrDefault(c=>c.agreement_id==id);
            return list; 
        }

        public ContractPo FanAgree(int id)
        {
            var list = _systemCenterDbSoaContext.ContractPo.FirstOrDefault(c => c.contract_id == id);
            return list;
        }

        public bool UpdateContr(ContractPo info)
        {
            var list = _systemCenterDbSoaContext.ContractPo.FirstOrDefault(c => c.contract_id == info.contract_id);
            list.chai_sjsum = info.chai_sjsum;
            list.chai_sum = info.chai_sum;
            list.contract_adder = info.contract_adder;
            list.contract_arch = info.contract_arch;
            list.contract_area = info.contract_area;
            list.contract_conname = info.contract_conname;
            list.contract_const = info.contract_const;
            list.contract_contname = info.contract_contname;
            list.contract_contype = info.contract_contype;
            list.contract_first_people = info.contract_first_people;
            list.contract_first_tell = info.contract_first_tell;
            list.contract_first_type = info.contract_first_type;
            list.contract_id = info.contract_id;
            list.contract_jgtype = info.contract_jzfl;
            list.contract_many = info.contract_many;
            list.contract_projectname = info.contract_projectname;
            list.contract_projecttell = info.contract_projecttell;
            list.contract_reamk = info.contract_reamk;
            list.contract_serial = info.contract_serial;
            list.contract_sjmany = info.contract_sjmany;
            list.contract_textserial = info.contract_textserial;
            list.contract_time = info.contract_time;
            list.contract_zicompany = info.contract_zicompany;
            list.contract_zt = info.contract_zt;
            list.img_sjsum = info.img_sjsum;
            list.img_sjsum = info.img_sjsum;
            _systemCenterDbSoaContext.SaveChanges();
            return true;
        }
    }
}
